package cn.cs.mathgo.app.util;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class TokenUtils {
    @Autowired
    private ClientDetailsService clientDetailsService;
    @Autowired
    private AuthorizationServerTokenServices authorizationServerTokenServices;
    public OAuth2AccessToken getOAuth2AccessToken(String header, Authentication authentication,HttpServletRequest request) throws IOException {
        //没有client信息
    /*    if (header == null || !header.startsWith("Basic ") || !header.startsWith("bearer ")) {
            throw new UnapprovedClientAuthenticationException("请求头中无client信息");
        }*/
        /**
         * 构造OAuth2Request 第一步，从请求头获取clientId
         */
        //base64解码获取clientId、clientSecret
        String[] tokens = extractAndDecodeHeader(header, request);
        assert tokens.length == 2;

        String clientId = tokens[0];
        String clientSecret = tokens[1];
        /**
         * 构造OAuth2Request 第二步，根据clientId 获取ClientDetails对象
         */
        ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);

        if(clientDetails == null){
            throw new UnapprovedClientAuthenticationException("clientId对应的配置信息不存在，clientId:"+clientId);
        }else if(!StringUtils.equals(clientDetails.getClientSecret(), clientSecret)){
            throw new UnapprovedClientAuthenticationException("clientSecret不匹配，clientId:"+clientId);
        }

        /**
         * 构造OAuth2Request 第三步，new TokenRequest
         * 第一个参数是个map，放的是每个授权模式特有的参数，springsecurity会根据这些参数构建Authentication
         * 我们这里已获取到了Authentication，弄个空的map就可
         */
        TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, clientId, clientDetails.getScope(), "custom");

        /**
         * 构造OAuth2Request 第四步，创建 OAuth2Request
         */
        OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
        /**
         * 构造OAuth2Request 第五步，创建 OAuth2Authentication
         */
        OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
        /**
         * 构造OAuth2Request 第六步，authorizationServerTokenServices创建 OAuth2AccessToken
         */
        return    authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
    }

    private String[] extractAndDecodeHeader(String header, HttpServletRequest request)
            throws IOException {
        //Basic aW1vb2M6aW1vb2NzZWNyZXQ= 截取Basic后的
        byte[] base64Token = header.substring(6).getBytes("UTF-8");
        byte[] decoded;
        try {
            //解码后格式   用户名:密码
            decoded = Base64.decode(base64Token);
        }
        catch (IllegalArgumentException e) {
            throw new BadCredentialsException(
                    "Failed to decode basic authentication token");
        }

        String token = new String(decoded, "UTF-8");

        int delim = token.indexOf(":");

        if (delim == -1) {
            throw new BadCredentialsException("Invalid basic authentication token");
        }
        //返回的数组是   [用户名(就是client_id),clientSecret] 其实就是配置的
        /**
         * security.oauth2.client.clientId = imooc
         security.oauth2.client.clientSecret = imoocsecret
         */
        return new String[] { token.substring(0, delim), token.substring(delim + 1) };
    }


}
